package dao.impl;

import Dto.GoodsDto;
import cqcet.aibd.soft.ObjectUtil;
import dao.OrderDAO;
import model.Order;
import model.common.Entity;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.util.List;


public class OrderDAOImpl implements OrderDAO {

    @Override
    public void addOrder(Connection con, Order order) throws Exception {
        PreparedStatement ps = null;

        String sql = "insert into `order` values(?,?,?,?,?,?,?,?,?,?,?)";
        ps = con.prepareStatement(sql);
        ps.setString(1, Entity.getUUID());
        ps.setString(2, order.getSerialNumber());
        ps.setString(3, "1");
        ps.setDate(4,  new Date(System.currentTimeMillis()));
        ps.setDate(5,  null);
        ps.setString(6, order.getUserId());
        ps.setString(7, order.getConsignee());
        ps.setString(8, order.getConsigneeAddress());
        ps.setString(9, order.getPhone());
        ps.setDouble(10, order.getAmount());
        ps.setString(11, order.getState());
        ps.executeUpdate();


    }


    @Override
    public void addOrder(Order order) {
        String sql = "insert into `order` values(?,?,?,?,?,?,?,?,?,?,?)";

        new ObjectUtil<Order>().add(sql, Entity.getUUID(), order.getSerialNumber(), "1", order.getCreateTime(), order.getDeleteTime(),
                order.getUserId(), order.getConsignee(), order.getConsigneeAddress(), order.getPhone(), order.getAmount(), order.getState());
    }

    @Override
    public List<Order> findOrder() throws Exception {
        String sql = "select * from `order`";
        return new ObjectUtil<Order>().getList(sql, Order.class);
    }

    @Override
    public void addOrderDetail(Order order, GoodsDto goodsDto) {
        String sql = "insert into order_detail values(?,?,?,?,?,?,?,?,?)";
        new ObjectUtil<Order>().add(sql, Entity.getUUID(), order.getSerialNumber(), goodsDto.getId(), goodsDto.getName(),
                goodsDto.getNum(), goodsDto.getPrice(), "1", order.getCreateTime(), order.getDeleteTime());
    }

    @Override
    public void deleteByOrderId(String id) {
        String sql = "delete from `order` where id=?";
        new ObjectUtil<Order>().delete(sql, id);
    }


    @Override
    public List<Order> findMyOrder(String id) {
        String sql = "select * from `order` where user_id=?";
        return new ObjectUtil<Order>().getList(sql, Order.class, id);
    }
}
